home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 23 / AMIGAplus Sonderheft 23 (2000)(Falke)(DE)[!].iso / Updates / Datatypes / RGFX-DT / Source / dispatcher.c < prev    next >
C/C++ Source or Header  |  1999-11-14  |  23KB  |  899 lines

  1. #include <clib/alib_protos.h>
  2. #include <pragma/datatypes_lib.h>
  3. #include <pragma/dos_lib.h>
  4. #include <pragma/exec_lib.h>
  5. #include <pragma/graphics_lib.h>
  6. #include <pragma/iffparse_lib.h>
  7. #include <pragma/intuition_lib.h>
  8. #include <pragma/render_lib.h>
  9. #include <pragma/utility_lib.h>
  10. #include <pragma/xpkmaster_lib.h>
  11. #include <render/renderhooks.h>
  12. #include <cybergraphx/cybergraphics.h>
  13. #include <datatypes/pictureclass.h>
  14. #include <exec/memory.h>
  15. #include <intuition/icclass.h>
  16. #include <string.h>
  17. #include "class.h"
  18. #include "rgfx.h"
  19.  
  20. struct Arg1
  21. {
  22. long *mode,*dither,*depth;
  23. char *xpk;
  24. long *xpkmode;
  25. char *password;
  26. };
  27.  
  28. ULONG SaveRGFX(IClass *cl,Object *obj,dtWrite *msg);
  29.  
  30. extern Library *SuperClassBase;
  31. extern Library *GfxBase;
  32.  
  33. static long iff2dos[]=
  34. {
  35. 0,
  36. 0,
  37. DTERROR_INVALID_DATA,
  38. ERROR_NO_FREE_STORE,
  39. ERROR_SEEK_ERROR,
  40. ERROR_SEEK_ERROR,
  41. ERROR_SEEK_ERROR,
  42. DTERROR_INVALID_DATA,
  43. DTERROR_INVALID_DATA,
  44. ERROR_OBJECT_WRONG_TYPE,
  45. ERROR_REQUIRED_ARG_MISSING,
  46. 0xDEADDEAD
  47. };
  48.  
  49. static long chunks[]=
  50. {
  51. ID_RGFX,ID_RGHD,
  52. ID_RGFX,ID_RSCM,
  53. ID_RGFX,ID_RCOL,
  54. ID_RGFX,ID_NAME,
  55. ID_RGFX,ID_AUTH,
  56. ID_RGFX,ID_ANNO,
  57. ID_RGFX,ID_Copyright,
  58. ID_RGFX,ID_FVER
  59. };
  60.  
  61. void ReadPrefs(Data *data)
  62. {
  63. char *var;
  64. data->mode=1;
  65. data->dither=0;
  66. data->depth=8;
  67. strcpy(data->xpk,"NUKE");
  68. data->xpkmode=100;
  69. if(data->password)
  70.     {
  71.     FreeVec(data->password);
  72.     data->password=0;
  73.     }
  74. if(var=(char *)AllocVec(256,0))
  75.     {
  76.     if(GetVar("ENV:DataTypes/rgfx.prefs",var,256,LV_VAR|GVF_GLOBAL_ONLY)>=0)
  77.         {
  78.         RDArgs *rdargs;
  79.       if(rdargs=(RDArgs *)AllocDosObject(DOS_RDARGS,0))
  80.             {
  81.             RDArgs *args;
  82.             Arg1 para;
  83.             rdargs->RDA_Source.CS_Buffer=var;
  84.             rdargs->RDA_Source.CS_Length=strlen(var);
  85.             rdargs->RDA_Source.CS_CurChr=0;
  86.             memset(¶,0,sizeof(Arg1));
  87.             if(args=ReadArgs("MODE/A/N,DITHER/A/N,DEPTH/A/N,XPK/A,XPKMODE/A/N,PASSWORD",(long *)¶,rdargs))
  88.                 {
  89.                 data->mode=*para.mode;
  90.                 if(data->mode<0) data->mode=0;
  91.                 else if(data->mode>1) data->mode=1;
  92.                 data->dither=*para.dither;
  93.                 if(data->dither<0) data->dither=0;
  94.                 else if(data->dither>1) data->dither=1;
  95.                 data->depth=*para.depth;
  96.                 if(data->depth<3) data->depth=3;
  97.                 else if(data->depth>8) data->depth=8;
  98.                 strncpy(data->xpk,para.xpk,4);
  99.                 data->xpk[4]=0;
  100.                 data->xpkmode=*para.xpkmode;
  101.                 if(data->xpkmode<0) data->xpkmode=0;
  102.                 else if(data->xpkmode>100) data->xpkmode=100;
  103.                 if(para.password)
  104.                     {
  105.                     if(data->password=(char *)AllocVec(strlen(para.password)+1,MEMF_CLEAR)) strcpy(data->password,para.password);
  106.                     }
  107.                 FreeArgs(args);
  108.                 }
  109.             FreeDosObject(DOS_RDARGS,rdargs);
  110.             }
  111.         }
  112.     FreeVec(var);
  113.     }
  114. if(SuperClassBase->lib_Version<43) data->mode=0;
  115. }
  116.  
  117. ULONG Colors2DT(Object *obj,UBYTE *color)
  118. {
  119. UBYTE *cr;
  120. ULONG *cregs,i,num,error=0;
  121. GetDTAttrs(obj,PDTA_NumColors,&num,PDTA_ColorRegisters,&cr,PDTA_CRegs,&cregs,TAG_END);
  122. if(cr&&cregs)
  123.     {
  124.     for(i=0;i<num;i++,cr+=3,cregs+=3,color+=4)
  125.         {
  126.         cr[0]=color[1];
  127.         cr[1]=color[2];
  128.         cr[2]=color[3];
  129.         cregs[0]=cr[0]<<24;
  130.         cregs[1]=cr[1]<<24;
  131.         cregs[2]=cr[2]<<24;
  132.         }
  133.     }
  134. else error=ERROR_NO_FREE_STORE;
  135. return error;
  136. }
  137.  
  138. static void GetRGHD(Object *obj,RGHD *rh,BitMapHeader *bh,Data *data)
  139. {
  140. bh->bmh_Left=rh->rgfx_LeftEdge;
  141. bh->bmh_Top=rh->rgfx_TopEdge;
  142. bh->bmh_Width=rh->rgfx_Width;
  143. bh->bmh_Height=rh->rgfx_Height;
  144. bh->bmh_PageWidth=rh->rgfx_PageWidth?rh->rgfx_PageWidth:bh->bmh_Width;
  145. bh->bmh_PageHeight=rh->rgfx_PageHeight?rh->rgfx_PageHeight:bh->bmh_Height;
  146. bh->bmh_Depth=(data->mode||rh->rgfx_Depth<=8)?rh->rgfx_Depth:data->depth;
  147. bh->bmh_Compression=cmpByteRun1;
  148. bh->bmh_XAspect=rh->rgfx_XAspect;
  149. bh->bmh_YAspect=rh->rgfx_YAspect;
  150. }
  151.  
  152. static ULONG GetRSCM(Object *obj,IFFHandle *iff,BitMapHeader *bh,Data *data,ULONG rgfxdepth)
  153. {
  154. ULONG mode=0,numc=0;
  155. if(rgfxdepth<=8||data->mode)
  156.     {
  157.     StoredProperty *sp;
  158.     mode=INVALID_ID;
  159.     if(sp=FindProp(iff,ID_RGFX,ID_RSCM))
  160.         {
  161.         RSCM *rscm=(RSCM *)sp->sp_Data;
  162.         if(!ModeNotAvailable(rscm->rscm_AGA))
  163.             {
  164.             ULONG dipf=0;
  165.             if(rscm->rscm_AGA&HAM_KEY)
  166.                 {
  167.                 numc=(bh->bmh_Depth==8)?64:16;
  168.                 dipf=DIPF_IS_HAM;
  169.                 }
  170.             else if(rscm->rscm_AGA&EXTRAHALFBRITE_KEY)
  171.                 {
  172.                 numc=32;
  173.                 dipf=DIPF_IS_EXTRAHALFBRITE;
  174.                 }
  175.             if(dipf) mode=BestModeID(
  176.             BIDTAG_NominalWidth,bh->bmh_PageWidth,BIDTAG_NominalHeight,bh->bmh_PageHeight,
  177.             BIDTAG_DesiredWidth,bh->bmh_Width,BIDTAG_DesiredHeight,bh->bmh_Height,BIDTAG_Depth,bh->bmh_Depth,
  178.             BIDTAG_DIPFMustHave,dipf,BIDTAG_DIPFMustNotHave,DIPF_IS_DUALPF|DIPF_IS_PF2PRI,TAG_END);
  179.             }
  180.         }
  181.     if(mode==INVALID_ID)
  182.         {
  183.         mode=BestModeID(
  184.         BIDTAG_NominalWidth,bh->bmh_PageWidth,BIDTAG_NominalHeight,bh->bmh_PageHeight,
  185.         BIDTAG_DesiredWidth,bh->bmh_Width,BIDTAG_DesiredHeight,bh->bmh_Height,
  186.         BIDTAG_Depth,bh->bmh_Depth,
  187.         BIDTAG_DIPFMustNotHave,DIPF_IS_DUALPF|DIPF_IS_PF2PRI|DIPF_IS_HAM|DIPF_IS_EXTRAHALFBRITE,TAG_END);
  188.         numc=1<<bh->bmh_Depth;
  189.         }
  190.     if(mode!=INVALID_ID)
  191.         {
  192.         SetDTAttrs(obj,0,0,PDTA_ModeID,mode,TAG_END);
  193.         if(numc) SetDTAttrs(obj,0,0,PDTA_NumColors,numc,TAG_END);
  194.         }
  195.     }
  196. else
  197.     {
  198.     if(!data->mode)
  199.         {
  200.         mode=BestModeID(
  201.         BIDTAG_NominalWidth,bh->bmh_PageWidth,BIDTAG_NominalHeight,bh->bmh_PageHeight,
  202.         BIDTAG_DesiredWidth,bh->bmh_Width,BIDTAG_DesiredHeight,bh->bmh_Height,
  203.         BIDTAG_Depth,bh->bmh_Depth,
  204.         BIDTAG_DIPFMustNotHave,DIPF_IS_DUALPF|DIPF_IS_PF2PRI|DIPF_IS_HAM|DIPF_IS_EXTRAHALFBRITE,TAG_END);
  205.         numc=1<<bh->bmh_Depth;
  206.         SetDTAttrs(obj,0,0,PDTA_ModeID,mode,PDTA_NumColors,numc,TAG_END);
  207.         }
  208.     }
  209. return mode;
  210. }
  211.  
  212. static ULONG GetRCOL(Object *obj,IFFHandle *iff,RGHD *rh)
  213. {
  214. ULONG error=0;
  215. if(rh->rgfx_Depth<=8)
  216.     {
  217.     StoredProperty *sp;
  218.     if(sp=FindProp(iff,ID_RGFX,ID_RCOL))
  219.         {
  220.         RCOL *rcol=(RCOL *)sp->sp_Data;
  221.         ULONG n,*cregs;
  222.         UBYTE *cr,*p;
  223.         GetDTAttrs(obj,PDTA_NumColors,&n,PDTA_ColorRegisters,&cr,PDTA_CRegs,&cregs,TAG_END);
  224.         if(cr&&cregs)
  225.             {
  226.             ULONG i;
  227.             n*=3;
  228.             for(i=0,p=rcol->rcol_Colors[0];i<n;i++,p++,cr++,cregs++)
  229.                 {
  230.                 *cr=*p;
  231.                 *cregs=(*p)<<24;
  232.                 }
  233.             }
  234.         else error=ERROR_NO_FREE_STORE;
  235.         }
  236.     else error=ERROR_REQUIRED_ARG_MISSING;
  237.     }
  238. return error;
  239. }
  240.  
  241. static ULONG GetBody_BP8(IFFHandle *iff,RGHD *rh,BitMap *bm,Data *data)
  242. {
  243. ULONG error=0,d;
  244. for(d=0;d<rh->rgfx_Depth;d++)
  245.     {
  246.     ULONG y;
  247.     UBYTE *pl=bm->Planes[d];
  248.     for(y=0;y<rh->rgfx_Height;y++,pl+=bm->BytesPerRow)
  249.         {
  250.         long err;
  251.         if((err=ReadChunkBytes(iff,pl,rh->rgfx_BytesPerLine)<0))
  252.             {
  253.             error=iff2dos[-err];
  254.             break;
  255.             }
  256.         }
  257.     }
  258. return error;
  259. }
  260.  
  261. static ULONG GetBody_BC8(IFFHandle *iff,RGHD *rh,BitMap *bm,Data *data)
  262. {
  263. ULONG error=0;
  264. long err;
  265. UBYTE *pix;
  266. if(pix=(UBYTE *)AllocVec(rh->rgfx_Width*rh->rgfx_Height,0))
  267.     {
  268.     if((err=ReadChunkBytes(iff,pix,rh->rgfx_Width*rh->rgfx_Height))<0) error=iff2dos[-err];
  269.     else Chunky2BitMap(pix,0,0,rh->rgfx_Width,rh->rgfx_Height,bm,0,0,TAG_END);
  270.     FreeVec(pix);
  271.     }
  272. else
  273.     {
  274.     BitMap *tbm;
  275.     if(tbm=AllocBitMap(rh->rgfx_Width,1,rh->rgfx_Depth,BMF_MINPLANES,bm))
  276.         {
  277.         if(pix=(UBYTE *)AllocVec(((rh->rgfx_Width+16)>>4)<<4,0))
  278.             {
  279.             ULONG y;
  280.             RastPort rp,trp;
  281.             InitRastPort(&rp);
  282.             InitRastPort(&trp);
  283.             rp.BitMap=bm;
  284.             trp.BitMap=tbm;
  285.             for(y=0;y<rh->rgfx_Height;y++)
  286.                 {
  287.                 long err;
  288.                 if((err=ReadChunkBytes(iff,pix,rh->rgfx_Width))<0)
  289.                     {
  290.                     error=iff2dos[-err];
  291.                     break;
  292.                     }
  293.                 WritePixelLine8(&rp,0,y,rh->rgfx_Width,pix,&trp);
  294.                 }
  295.             FreeVec(pix);
  296.             }
  297.         else error=ERROR_NO_FREE_STORE;
  298.         FreeBitMap(tbm);
  299.         }
  300.     else error=ERROR_NO_FREE_STORE;
  301.     }
  302. return error;
  303. }
  304.  
  305. static ULONG GetBody_RGB_V43(Object *obj,IFFHandle *iff,RGHD *rh,Data *data)
  306. {
  307. ULONG error=0;
  308. UBYTE *pix;
  309. long err;
  310. if(pix=(UBYTE *)AllocVec(rh->rgfx_BytesPerLine*rh->rgfx_Height,0))
  311.     {
  312.     if((err=ReadChunkBytes(iff,pix,rh->rgfx_BytesPerLine*rh->rgfx_Height))<0) error=iff2dos[-err];
  313.     else DoMethod(obj,PDTM_WRITEPIXELARRAY,pix,RECTFMT_RGB,rh->rgfx_BytesPerLine,0,0,rh->rgfx_Width,rh->rgfx_Height);
  314.     FreeVec(pix);
  315.     }
  316. else if(pix=(UBYTE *)AllocVec(rh->rgfx_BytesPerLine,0))
  317.     {
  318.     ULONG y;
  319.     for(y=0;y<rh->rgfx_Height;y++)
  320.         {
  321.         if((err=ReadChunkBytes(iff,pix,rh->rgfx_BytesPerLine))<0)
  322.             {
  323.             error=iff2dos[-err];
  324.             break;
  325.             }
  326.         DoMethod(obj,PDTM_WRITEPIXELARRAY,pix,RECTFMT_RGB,rh->rgfx_BytesPerLine,0,y,rh->rgfx_Width,1);
  327.         }
  328.     FreeVec(pix);
  329.     }
  330. else error=ERROR_NO_FREE_STORE;
  331. return error;
  332. }
  333.  
  334. static ULONG GetBody_RGB(Object *obj,IFFHandle *iff,RGHD *rh,BitMapHeader *bh,BitMap *bm,Data *data)
  335. {
  336. ULONG error=0;
  337. UBYTE *rgb;
  338. if(rgb=(UBYTE *)AllocV